package com.woke.fragment;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.BottomSheetDialogFragment;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ProgressBar;

import com.woke.adapter.OnItemClickListener;
import com.woke.diyview.PasswordView;
import com.woke.diyview.XNumberKeyboardView;
import com.zhongcai.online.R;

/**
 * 作者 ： 暗网
 * 创建时间：2017/3/28
 * 备注 密码验证弹窗
 */

public class PwdAuthBottomSheetDialogFragment extends BottomSheetDialogFragment implements XNumberKeyboardView.IOnKeyboardListener, PasswordView.TextWatcher {


    private OnItemClickListener mItemClickListener;
    private XNumberKeyboardView mKeyBoardView;
    private PasswordView mPwdView;
    private ProgressBar mLoadProgress;

    public void setAfterTextChangedListener(OnItemClickListener listener) {
        mItemClickListener = listener;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return super.onCreateDialog(savedInstanceState);
    }

    @Override
    public void setupDialog(Dialog dialog, int style) {
        super.setupDialog(dialog, style);
        Context context = dialog.getContext();

        View rootView = LayoutInflater.from(context)
                .inflate(R.layout.fragment_password_auth, null);
        Toolbar toolbar = (Toolbar) rootView.findViewById(R.id.toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        mPwdView = (PasswordView) rootView.findViewById(R.id.passwordView);
        mPwdView.setOnTextChangedListener(this);
        mKeyBoardView = (XNumberKeyboardView) rootView.findViewById(R.id.xNumKeyboardView);
        mKeyBoardView.setIOnKeyboardListener(this);
        mLoadProgress = (ProgressBar) rootView.findViewById(R.id.loadProgress);
        dialog.setContentView(rootView);
    }

    @Override
    public void onInsertKeyEvent(String text) {
        int tempLength = mPwdView.length();
        if (tempLength >= 6) {
            mPwdView.getText().subSequence(0, 6);
        } else {
            mPwdView.append(text);
        }
    }

    @Override
    public void onDeleteKeyEvent() {
        int tempLength = mPwdView.length();
        if (tempLength > 0) {
            mPwdView.setText(mPwdView.getText().subSequence(0, tempLength - 1));
        }
    }


    @Override
    public void afterTextChanged(CharSequence charSequence) {
        mKeyBoardView.setVisibility(View.INVISIBLE);
        mLoadProgress.setVisibility(View.VISIBLE);
        if (mItemClickListener != null) {
            mItemClickListener.OnItemClick(mPwdView, charSequence, -1);
        }
        mLoadProgress.postDelayed(new Runnable() {
            @Override
            public void run() {
                mKeyBoardView.setVisibility(View.VISIBLE);
                mLoadProgress.setVisibility(View.INVISIBLE);
                mPwdView.setText("");
                dismiss();
            }
        }, 3000);
    }

}
